<?php

/**
 * Created by PhpStorm.
 * User: bob
 * Date: 21/9/25
 * Time: 上午10:37
 */

namespace App\Api\Admin\Controllers;

use App\Api\Account\Models\ContactUs;
use App\Api\Account\Services\BusinessService;
use App\Api\Admin\Models\Tag;
use App\Api\Web\Models\House;
use App\Api\Web\Models\HouseTag;
use App\Api\Web\Models\HouseWanted;
use App\Api\Web\Services\HouseService;
use App\Http\Controllers\Controller;
use App\User;
use Carbon\Carbon;
use function GuzzleHttp\Promise\promise_for;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index(Request $request)
    {
        $params = $request->all();
        $pageSize = data_get($params, 'page_size', 20);
        $page = data_get($params, 'page', 1);
        $name = data_get($params, 'name', '');
        $housesWanted = User::leftJoin('user_accounts as ua', 'ua.user_id', '=', 'users.id')
            ->orderBy('users.id', 'desc')
            ->select('users.*', 'ua.balance', 'ua.integral', 'ua.freeze');
        if (!empty($name) || $name == '0') {
            $housesWanted = $housesWanted->where('users.name', 'like', "%$name%");
        }
        $housesWanted = $housesWanted
            ->paginate($pageSize, ['*'], 'page', $page)
            ->toArray();

        return $this->formatOutput($housesWanted);
    }

    public function certIndex(Request $request)
    {
        $params = $request->all();
        $pageSize = data_get($params, 'page_size', 20);
        $page = data_get($params, 'page', 1);
        $r = User::where('cert_status', '!=', 0)
            ->orderBy('users.id', 'desc')
            ->paginate($pageSize, ['*'], 'page', $page)
            ->toArray();

        foreach ($r['data'] as  $k => $v) {
            $r['data'][$k]['cert_status_name'] = data_get(User::$cert_status, data_get($v, 'cert_status', ''), '未知');
        }

        return $this->formatOutput($r);
    }

    public function userUpdate(Request $request)
    {
        $params = $request->all();
        $id = data_get($params, 'id', '');
//        $id = data_get($params, 'id', '');
        if (empty($id)) {
            throw new \LogicException('缺少必要参数', 100);
        }
        $updates = [];
        $name  = data_get($params, 'name', '');
        $real_name  = data_get($params, 'real_name', '');
        $head_img  = data_get($params, 'head_img', '');
        $sex  = data_get($params, 'sex', '');
        $birth  = data_get($params, 'birth', '');
        $living  = data_get($params, 'living', '');
        $interest  = data_get($params, 'interest', '');
        $character  = data_get($params, 'character', '');
        $signature  = data_get($params, 'signature', '');
        $bank  = data_get($params, 'bank', '');
        $bank_person  = data_get($params, 'bank_person', '');
        $bank_account  = data_get($params, 'bank_account', '');
        $alipay  = data_get($params, 'alipay', '');
        $back  = data_get($params, 'back', '');
        $front  = data_get($params, 'front', '');
        $idNum  = data_get($params, 'id_num', '');
        $aquarius  = data_get($params, 'aquarius', '');
        $city  = data_get($params, 'city', '');
        $cert_status  = data_get($params, 'cert_status', '');

        $photos = data_get($params, 'photos', []);
        if (is_array($photos) && !empty($photos)) {
            foreach ($photos as $k => $photo) {
                $photos[$k] = $photo;
            }
            $photosStr = json_encode($photos);
            empty($photosStr) ? : $updates['photos'] = $photosStr;
        }


        empty($name) ? : $updates['name'] = $name;
        empty($real_name) ? : $updates['real_name'] = $real_name;
        empty($photos) ? : $updates['photos'] = $photos;
        empty($head_img) ? : $updates['head_img'] = $head_img;
        empty($sex) ? : $updates['sex'] = $sex;
        empty($birth) ? : $updates['birth'] = $birth;
        empty($living) ? : $updates['living'] = $living;
        empty($interest) ? : $updates['interest'] = $interest;
        empty($character) ? : $updates['character'] = $character;
        empty($signature) ? : $updates['signature'] = $signature;
        empty($bank_account) ? : $updates['bank_account'] = $bank_account;
        empty($bank) ? : $updates['bank'] = $bank;
        empty($bank_person) ? : $updates['bank_person'] = $bank_person;
        empty($alipay) ? : $updates['alipay'] = $alipay;
        empty($back) ? : $updates['back'] = $back;
        empty($front) ? : $updates['front'] = $front;
        if (!empty($idNum)) {
            $updates['id_num'] = $idNum;
        }
        empty($cert_status) ? : $updates['cert_status'] = $cert_status;
        empty($aquarius) ? : $updates['aquarius'] = $aquarius;
        empty($city) ? : $updates['city'] = $city;

        if (!empty($updates)) {
            User::find($id)
                ->update($updates);
        }


        return $this->formatOutput('success');

    }

    public function balanceUpdate(Request $request)
    {
        $params = $request->all();
        $userId = data_get($params, 'user_id', '');
        $total = data_get($params, 'total', '');
        if (empty($userId) || empty($total)) {
            throw new \LogicException('缺少必要参数', 100);
        }

        $r = BusinessService::commonTransaction($userId, $total, 0, 'admin', '管理员划扣');

        return $this->formatOutput($r);

    }

    public function integralUpdate(Request $request)
    {
        $params = $request->all();
        $userId = data_get($params, 'user_id', '');
        $total = data_get($params, 'total', '');
        if (empty($userId) || empty($total)) {
            throw new \LogicException('缺少必要参数', 100);
        }

        $r = BusinessService::commonIntegralTransaction($userId, $total, 0, 'admin', '管理员划扣');

        return $this->formatOutput($r);

    }

    public function adminContactUsIndex(Request $request)
    {
        $params = $request->all();
        $pageSize = data_get($params, 'page_size', 20);
        $page = data_get($params, 'page', 1);
        $mobile = data_get($params, 'mobile', '');
//        $text = data_get($params, 'text', '');
        $type = data_get($params, 'type', '');



        $r = ContactUs::leftJoin('users as u', 'u.id', '=', 'contact_us.user_id')
            ->select('contact_us.*', 'u.name as username', 'u.head_img as user_head_img');
//            ->leftJoin('users as ur', 'ur.id', '=', 'contact_us')

        if ($type == 1) {
            $r = $r->leftJoin('users as ur', 'ur.id', '=', 'contact_us.report_user_id')
                ->addSelect('ur.name as report_username', 'ur.mobile as report_mobile');
        }
        if (!empty($mobile)) {
            $r = $r->where('contact_us.mobile', 'like', "%$mobile%");
        };
        if (!empty($type)) {
            $r = $r->where('contact_us.type', '=', $type);
        };
        $r = $r

            ->paginate($pageSize, ['*'], 'page', $page)
            ->toArray();

        foreach ($r['data'] as $k => $v) {
            $r['data'][$k]['type_name'] = data_get(ContactUs::$type, data_get($v, 'type', ''), '未知');
//            $r['data'][$k]['type_name'] = data_get(ContactUs::$type, data_get($v, 'type', ''), '未知');
        }

        return $this->formatOutput($r);

    }

    public function userInvited(Request $request)
    {
        $params = $request->all();
        $userId = data_get($params, 'user_id', '');
        $page = data_get($params, 'page', '');
        $pageSize = data_get($params, 'page_size', '');
        // users 是普通用户，  u 是邀请别人的人
        $m = User::join('users as u', 'u.id', '=', 'users.share_user_id')
            ->select('users.name as shared_name', 'u.name', 'u.id', 'users.id as shared_id', 'users.be_shared_at');

        if (!empty($userId)) {
            $m = $m->where('u.id', '=', $userId);
        }

        $m = $m->orderBy('u.id', 'desc')
            ->paginate($pageSize, ['*'], 'page', $page)
            ->toArray();

        return $this->formatOutput($m);
    }
}